package com.yuandian.customer.perception.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuandian.api.customer_perception.po.BusinessStatisticsParams;
import com.yuandian.api.customer_perception.po.DayReportParams;
import com.yuandian.api.customer_perception.vo.BusinessStatisticsVo;
import com.yuandian.api.customer_perception.vo.ReportBusinessFailStatisticsTopVo;
import com.yuandian.api.customer_perception.vo.ReportBusinessStatisticsTopVo;
import com.yuandian.bpm.common.core.util.R;
import com.yuandian.customer.perception.mapper.BusinessStatisticsMapper;
import com.yuandian.customer.perception.service.IBusinessStatisticsService;
import com.yuandian.enums.TableNameEnum;
import com.yuandian.utils.PageDataInfo;
import com.yuandian.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 说明：业务统计service实现
 * @Author yanjun
 * @Date 2021/3/17 15:35
 */
@Service
public class BusinessStatisticsServiceImpl implements IBusinessStatisticsService {

    @Autowired
    private BusinessStatisticsMapper businessStatisticsMapper;

    /**
     * 查询业务统计列表
     * @param params 业务统计请求参数对象
     * @return 通用返回对象
     */
    @Override
    public R queryBusinessStatisticsList(BusinessStatisticsParams params) {
        // 1、设置表名
        List<String> tableNameList =new ArrayList<>();
		tableNameList.add("busi_sys_detail_stat_m");
        params.setTableNameList(tableNameList);
        // 2、设置分页参数
        Page<BusinessStatisticsVo> page = new Page<>(params.getPageNum(), params.getPageSize());
        // 3、执行查询
        List<BusinessStatisticsVo> list = businessStatisticsMapper.queryBusinessStatisticsList(params, page);
        return R.ok(new PageDataInfo(page, list));
    }
    /**
     * 查询业务统计列表的业务编码和业务编号-不分页
     * @param params 业务统计请求参数对象
     * @return 通用返回对象
     */
    @Override
    public R queryBusinessStatisticsListNoPage(BusinessStatisticsParams params) {
        // 1、设置表名
        List<String> tableNameList = new ArrayList<>();
		tableNameList.add("busi_sys_detail_stat_m");
        params.setTableNameList(tableNameList);
        // 2、设置分页参数
        Page<BusinessStatisticsVo> page = new Page<>(params.getPageNum(), params.getPageSize());
        // 3、执行查询
        List<BusinessStatisticsVo> list = businessStatisticsMapper.queryBusinessStatisticsListNoPage(params);
        return R.ok(list);
    }

    /**
     * 查询运维日报业务统计(地市)topN
     * @param params 运维日报请求参数
     * @return 通用返回对象
     */
    @Override
    public R queryReportBusinessAreaTopN(DayReportParams params) {
        // 1、设置表名
		params.setTableNameList(Collections.singletonList(TableNameEnum.ADS_BUSI_SYS_VISIT_TREND_M.getName()));
        // 2、设置默认排序
        if (ObjectUtil.isEmpty(params.getIsAsc())) {
            params.setIsAsc("desc");
        }
        // 3、设置排序字段
        switch (params.getStatisticsType()) {
            case VISIT_COUNT:
                params.setOrderByColumn("visitCount");
                break;
            case COMPLETE_COUNT:
                params.setOrderByColumn("completeCount");
                break;
            case FAIL_COUNT:
                params.setOrderByColumn("failCount");
                break;
            case FAIL_RATE:
                params.setOrderByColumn("failRate");
                break;
        }
        // 4、执行查询
        List<ReportBusinessStatisticsTopVo> list = businessStatisticsMapper.queryReportBusinessAreaTopN(params);
        return R.ok(list);
    }

    /**
     * 查询运维日报业务统计(营业厅)topN
     * @param params 运维日报请求参数
     * @return 通用返回对象
     */
    @Override
    public R queryReportBusinessHallTopN(DayReportParams params) {
        // 1、设置表名
        params.setTableNameList(Collections.singletonList(TableNameEnum.ADS_BUSI_SYS_VISIT_TREND_M.getName()));
        // 2、设置默认排序
        if (StringUtils.isEmpty(params.getIsAsc())) {
            params.setIsAsc("desc");
        }
        // 3、设置排序字段
        switch (params.getStatisticsType()) {
            case VISIT_COUNT:
                params.setOrderByColumn("visitCount");
                break;
            case COMPLETE_COUNT:
                params.setOrderByColumn("completeCount");
                break;
            case FAIL_COUNT:
                params.setOrderByColumn("failCount");
                break;
            case FAIL_RATE:
                params.setOrderByColumn("failRate");
                break;
        }
        // 4、执行查询
        List<ReportBusinessStatisticsTopVo> list = businessStatisticsMapper.queryReportBusinessHallTopN(params);
        return R.ok(list);
    }

    /**
     * 查询运维日报业务失败统计topN
     * @param params 运维日报请求参数
     * @return 通用返回对象
     */
    @Override
    public R queryReportBusinessFailTopN(DayReportParams params) {
        // 1、设置表名
        params.setTableNameList(Collections.singletonList(TableNameEnum.ADS_BUSI_SYS_VISIT_TREND_M.getName()));
        // 2、设置默认排序
        if (StringUtils.isEmpty(params.getIsAsc())) {
            params.setIsAsc("desc");
        }
        // 3、设置排序字段
        switch (params.getFailType()) {
            case FAIL_COUNT:
                params.setOrderByColumn("failCount");
                break;
            case FAIL_RATE:
                params.setOrderByColumn("failRate");
                break;
        }
        // 4、执行查询
        List<ReportBusinessFailStatisticsTopVo> list = businessStatisticsMapper.queryReportBusinessFailTopN(params);
        return R.ok(list);
    }
}
